package com.xw.musicplayer.focus;

import android.content.Context;
import android.media.AudioManager;

/**
 * Api21焦点管理.
 *
 * @author ingxin
 * @date 2020/5/24
 */
public class BaseAudioFocusHelper21 extends BaseAudioFocusHelper {

    private AudioManager mAudioManager;

    public BaseAudioFocusHelper21(AudioManager.OnAudioFocusChangeListener listener) {
        super(listener);
    }

    @Override
    public void request(Context context) {
        if (mAudioManager == null) {
            mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        }
        //忽略请求焦点
        if (mAudioManager != null) {
            int requestRes = mAudioManager.requestAudioFocus(mFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
        }

    }

    @Override
    public void abandon() {
        if (mAudioManager != null) {
            mAudioManager.abandonAudioFocus(mFocusChangeListener);
        }
    }


}
